package com.peng.leetcode.tree;

/**
 * IsSubStructure1
 * 剑指 Offer 26. 树的子结构
 * https://leetcode.cn/problems/shu-de-zi-jie-gou-lcof/
 * <p>
 * Created on 2022/11/20
 *
 * @author lupeng
 */
public class IsSubStructure1 {

    public static void main(String[] args) {
//        [10,12,6,8,3,11]
//[10,12,6,8]
        Integer[] A = {10,12,6,8,3,11}, B = {10,12,6,8};
        TreeNode treeA = TreeMain.array2tree(A);
        TreeNode treeB = TreeMain.array2tree(B);
        System.out.println(new IsSubStructure1().isSubStructure(treeA, treeB));
    }

    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) {
            return false;
        }
        return judge(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    private boolean judge(TreeNode a, TreeNode b) {
        if (b == null) {
            return true;
        }
        if (a == null || a.val != b.val) {
            return false;
        }
        return judge(a.left, b.left) && judge(a.right, b.right);
    }
}
